<?php
/**
 * 订单操作控制
 * @author tangbl
 */
class OrderAction extends CommonAction{
	
	public function _initialize() {
	
		parent::_initialize();
		//parent::checkMBAC();
	}
	
	/**
	 * 订单列表
	 */
	public function order_index(){
		
		$status  = $this->_get("status","intval",0);
		$order   = getConfig("order");
		$user    = session(C('USER_AUTH_OBJECT'));
		
		$url     = "Order/order_index";
		$target  = "detail_order";
		$pagesid = "page_order";
		
		$table   = $order["table"];
		$where   = $order["where"];
		$filter  = $order["filter"];
		$order   = $order["order"];
		$model   = D("$table");
		
		$role = session(C('USER_AUTH_TIME_VIEW')); //时间权限
		//$map  = $model->getUserRole($role);
		
		if(is_array($filter)){
			foreach($filter as $k=>$v){
				$map[$k] = $user[$v] ;
			}
		}
		if(is_array($where)){
			foreach($where as $k=>$v){
				$map[$k] = $v ;
			}
		}
		if($status){
			$map['status'] = $status;
		}
		
		import("ORG.Util.AjaxPage");
		$param     = http_build_query($map);
		$nowPage   = $this->_get("p","intval",1);
		$pageNum   = 10 ;
		$count     = $model->where($map)->count();
		
		$totalPage = ceil($count/$pageNum);
		if($nowPage>$totalPage){
			$nowPage = $totalPage;
		}
		$Page      = new AjaxPage($count,$pageNum, $param,$url,$target,$pagesid);
		$list      = $model->where($map)->order($order)
					->limit((intval($nowPage)-1)*$pageNum,$pageNum)->select();
		
		//查询酒水
		$detail = M('OrderDetail');
		foreach($list as $k=>$v){
			$where['flag']    = 'drink';
			$where['orderno'] = $v['orderno'];
			$rows = $detail->where($where)->select();
			
			$list[$k]['list'] = $rows;
		}
		
		//赠送的饮料
		$this->assign("sdrink",  C("IS_SEND_DRINK"));
		
		//查询餐厅
		$module = M('Outlet');
		foreach($list as $k=>$v){
			$one = $module->find($v['outletid']);
			$list[$k]['outletname'] = $one['title'];
		}
		
		$data["page"]  = $Page->show();
		$data['list']  = $list;
		$data['total'] = $count;
		$data['param'] = $param;
		$epmty = "<tr><td colspan='6' align='center'>".L("ord_form_page_tip")."</td></tr>";
		$this->assign("empty",$epmty);
		$this->assign("order",$data);
		
		if(IS_AJAX){
			exit($this->fetch("order_order"));
		}else{
			$select  = getConfig("select");
			$this->assign("select",$select);
			$this->assign("map",$map);
			$this->display();
		}
	}
	
	/**
	 * 配送员到达餐厅5-15
	 */
	public function order_daoda(){
		
		$id    = $this->_param("id","intval",0);
		$model = D("Order");
		$row   = $model->find($id);
		if(!$row['id']) $this->error(L("ord_allot_error_tip"));

		//修改订单状态
		$map['status']   = 15;
		$map['id']       = $row['id'];
		$map['time_dao'] = time();
		$model->save($map);
			
		//添加订单操作记录
		$cat['oldid']   = $row['id'];
		$cat['orderno'] = $row['orderno'];
		$cat['status']  = 9;
		$cat['remark']  = "配送员已到达餐厅";
		$data = $model->saveLog($cat);
		$this->ajaxReturn(array("status"=>1,"data"=>$data,"info"=>"订单状态修改成功，当前状态：已到达餐厅"));
	}
	
	/**
	 * 配送员领取订单2-3
	 */
	public function order_allot(){
		
		$id    = $this->_param("id","intval",0);
		$model = D("Order");
		$row   = $model->find($id);
		if(!$row['id']) $this->error();
		
		if(IS_AJAX){
			
			//修改订单状态
			$map['dealid']   = session(C('USER_AUTH_KEY'));
			$map['status']   = 3;
			$map['id']       = $row['id'];
			$model->save($map);
			
			//添加订单操作记录
			$cat['oldid']   = $row['id'];
			$cat['orderno'] = $row['orderno'];
			$cat['status']  = 3;
			$cat['field']   = "status,dealid";
			$cat['title']   = "订单分配到配送员";
			$cat['content'] = "配送员自领订单";
			$cat['dataid']  = $row['dataid'];
			$cat['tableid'] = $row['tableid'];
			$data = $model->saveLog($cat);
			$this->ajaxReturn(array("status"=>1,"data"=>$data,"info"=>"领取订单成功"));
		}else{
			
			$lang    = getLang();
			$options = getDictByKey("Dict_address",$lang);
			$this->assign("json",$options["array"]);
			$this->assign("row",$row);
			$this->display();
		}
	}
	
	/**
	 * 订单配送完成 4-5
	 */
	public function order_delivery(){
		
		$id    = $this->_param("id","intval",0);
		$model = D("Order");
		$row   = $model->find($id);
		if(!$row['id']) $this->error(L("ord_allot_error_tip"));
		
		if(IS_AJAX){
			
			//修改订单状态
			$map['status'] = 10;
			$map['id']     = $row['id'];
			$model->save($map);
				
			//添加订单操作记录
			$cat['oldid']   = $row['id'];
			$cat['orderno'] = $row['orderno'];
			$cat['status']  = 10;
			$cat['field']   = "status";
			$cat['title']   = "订单配送完成";
			$cat['content'] = "订单配送完成";
			$cat['dataid']  = $row['dataid'];
			$cat['tableid'] = $row['tableid'];
			$data = $model->saveLog($cat);
			$this->ajaxReturn(array("status"=>1,"data"=>$data,"info"=>"订单状态修改成功，当前状态：配送完成"));
		}else{
			
			$outlet = session("outlet");
			$map['role']     = 9;
			$map['outletid'] = $outlet['id'];
			$user   = $model->getUserRoleOutle($map);
			
			$this->assign("deliverys",$user);
			
			$this->assign("row",$row);
			$this->display();
		}
	}
	
	/**
	 * 退回订单 2-1
	 */
	public function order_revert(){
		
		$id    = $this->_param("id","intval",0);
		$model = D("Order");
		$row   = $model->find($id);
		if(!$row['id']) $this->error(L("ord_allot_error_tip"));
		
		$map['addid'] = session(C('USER_AUTH_KEY'));
		$map['id']    = $row['id'];
		$map['userid']= $row['userid'];
		$map['status']= 1;
		$json = sendRest("Order/userStatus",$map);
		
		$this->ajaxReturn($json);
	}
	
	/**
	 * 已取餐 5-9
	 */
	public function order_hasget(){
		
		$id    = $this->_param("id","intval",0);
		$model = D("Order");
		$row   = $model->find($id);
		if(!$row['id']) $this->error(L("ord_allot_error_tip"));

		//修改订单状态
		$map['status']   = 9;
		$map['id']       = $row['id'];
		$map['time_pei'] = time();
		$model->save($map);
			
		//添加订单操作记录
		$cat['oldid']   = $row['id'];
		$cat['orderno'] = $row['orderno'];
		$cat['status']  = 9;
		$cat['remark']  = "配送员已取餐";
		$data = $model->saveLog($cat);
		$this->ajaxReturn(array("status"=>1,"data"=>$data,"info"=>"订单状态修改成功，当前状态：已取餐"));
	}
	
	/**
	 * 确认收货 9-10
	 */
	public function order_send(){
		
		$id    = $this->_param("id","intval",0);
		$model = D("Order");
		$row   = $model->find($id);
		if(!$row['id']) $this->error(L("ord_allot_error_tip"));
		
		if(IS_AJAX){
			//修改支付状态
			$paytype = $this->_param("paytype","trim",'');
			$model->save(array("id"=>$row['id'],"paytype"=>$paytype,"ispay"=>1));
			
			//修改订单状态
			$map['status']    = 10;
			$map['id']        = $row['id'];
			$map['time_shou'] = time();
			$model->save($map);
				
			//添加订单操作记录
			$cat['oldid']   = $row['id'];
			$cat['orderno'] = $row['orderno'];
			$cat['status']  = 10;
			$cat['remark'] = "订单已送达，客户已签收";
			$data = $model->saveLog($cat);
			$this->ajaxReturn(array("status"=>1,"data"=>$data,"info"=>"订单状态修改成功，当前状态：已送达"));
		}else{
			
			$this->assign("row",$row);
			$this->display();
		}	
	}
	
	/**
	 * 客户拒签 9-11
	 */
	public function order_refused(){
		
		$id    = $this->_param("id","intval",0);
		$model = D("Order");
		$row   = $model->find($id);
		if(!$row['id']) $this->error(L("ord_allot_error_tip"));
		
		if(IS_AJAX){
			//保存拒签原因
			$refused = $this->_param("refused","trim",'');
			$model->save(array("id"=>$row['id'],"refused"=>$refused));

			//修改订单状态
			$map['status']    = 11;
			$map['id']        = $row['id'];
			$map['time_shou'] = time();
			$model->save($map);
			
			//添加订单操作记录
			$cat['oldid']   = $row['id'];
			$cat['orderno'] = $row['orderno'];
			$cat['status']  = 11;
			$cat['remark'] = "客户拒签，".$refused;
			$data = $model->saveLog($cat);
			$this->ajaxReturn(array("status"=>1,"data"=>$data,"info"=>"订单状态修改成功，当前状态：客户拒签"));
		}else{
			$this->assign("row",$row);
			$this->display();
		}
	}
	
	/**
	 * 订单备注
	 */
	public function order_bak(){
		
		$id    = $this->_param("id","intval",0);
		$model = D("Order");
		$row   = $model->find($id);
		if(!$row['id']) $this->error(L("ord_allot_error_tip"));
		
		if(IS_AJAX){
			$map = $_POST;
			$map['did']     = $id;
			$map['addtime'] = time();
			$map['addid']   = session(C('USER_AUTH_KEY'));
			unset($map['id']);
			$data = M('OrderBak')->add($map);
			
			if($data){
				$this->ajaxReturn(array('status'=>1));
			}else{
				$this->ajaxReturn(array('status'=>-1,'info'=>'添加订单备注失败'));
			}
		}else{
			$this->assign("row",$row);
			$this->display();
		}
	}
	
	/**
	 * 取消订单 *->-1
	 */
	public function order_close(){
	
		$id    = $this->_param("id","intval",0);
		$model = D("Order");
		$row   = $model->find($id);
		if(!$row['id']) $this->error(L("ord_allot_error_tip"));
		
		/*
		$map['addid'] = session(C('USER_AUTH_KEY'));
		$map['id']    = $row['id'];
		$map['userid']= $row['userid'];
		$map['status']= 6;
		$json = sendRest("Order/userStatus",$map);
		*/
		
		//修改订单状态
		$map['status'] = -1;
		$map['id']     = $row['id'];
		$model->save($map);
			
		//添加订单操作记录
		$cat['oldid']   = $row['id'];
		$cat['orderno'] = $row['orderno'];
		$cat['status']  = -1;
		$cat['field']   = "status";
		$cat['title']   = "关闭订单";
		$cat['content'] = "关闭订单";
		$cat['dataid']  = $row['dataid'];
		$cat['tableid'] = $row['tableid'];
		$data = $model->saveLog($cat);
		$this->ajaxReturn(array("status"=>1,"data"=>$data,"info"=>"订单状态修改成功，当前状态：关闭订单"));
	}
	
	/**
	 * 打印订单
	 */
	public function order_print(){
		
		$id    = $this->_param("id","intval",0);
		$model = D("Order");
		$row   = $model->find($id);
		if(!$row['id']) $this->error(L("ord_allot_error_tip"));
	
		$m['userid'] = $row['userid'];
		$m['id']     = $row['id'];
		$m['detail'] = 1;
		
		$json = sendRest("Order/userDetail",$m);
		$this->assign("detail",$json['data']);
		
		$url = HTTP_URL.U("Order/detail","id=".$id);
		$url = ABS_URL."u.php?s=U/u/id/".$id;
		$this->assign("qrcode",urlencode($url));
	
		$this->display();
	}
	
	/**
	 * 订单详情
	 */
	public function order_detail(){
		
		$id    = $this->_param("id","intval",0);
		$model = D("Order");
		$row   = $model->find($id);
		if(!$row['id']) $this->error(L("ord_allot_error_tip"));
		
		$order   = getConfig("order");
		$user    = session(C('USER_AUTH_OBJECT'));
		
		$table   = $order["table"];
		$where   = $order["where"];
		$filter  = $order["filter"];
		$order   = $order["order"];
		
		if(is_array($filter)){
			foreach($filter as $k=>$v){
				$map[$k] = $user[$v] ;
			}
		}
		if(is_array($where)){
			foreach($where as $k=>$v){
				$map[$k] = $v ;
			}
		}
		$status  = $this->_param("status","intval",0);
		if($status){
			$map['status'] = $status;
		}
		
		$param       = http_build_query($map);
		$module      = D("$table");
		
		$map["id"]   = array("gt",$id);//>
		$preone      = $module->where($map)->order("addtime asc")->getField("id");
		
		if($preone){
			$data['pre'] = U("Order/order_detail","id=".$preone."&".$param);
		}
		
		$map["id"]   = array("lt",$id);//<
		$nextone     = $module->where($map)->order("addtime desc")->getField("id");
		
		if($nextone){
			$data['next']= U("Order/order_detail","id=".$nextone."&".$param);
		}
		$this->assign("data",$data);
		
		$act['orderno'] = $row['orderno'];
		$view = M("OrderDetail")->where($act)->select();
		
		foreach($view as $k=>$v){
			$view[$k]['attr'] = json_decode($v['attr'],true);
		}
		
		//查询餐厅
		$outlet = M('Outlet')->find($row['outletid']);
		$row['outletname'] = $outlet['title'];
		
		//免费饮品
		$drink = C('IS_SEND_DRINK');
		$this->assign('drink',$drink);

		//订单备注
		$arr = array('addid'=>$row['id']);
		$bak = M('OrderBak')->where($arr)->select();
		//echo M('OrderBak')->getLastSql();exit;
		$this->assign('orderbak',$bak);
		
		$this->assign("list",$view);
		$this->assign("detail",$row);
		$this->display();	
	}
	
	/**
	 * 数据统计
	 */
	public function order_total(){
		
		$model = D("Order");
		$begin = strtotime("today");
		$end   = $begin+86400;
		
		$map['dealid']  = session(C('USER_AUTH_KEY'));
		$map['addtime'] = array(array('egt',strtotime("today")),array('lt',$end));
		$data = $model->where($map)->select();
		
		$data = $model->getOrderTotal($data);
		$this->assign('row',$data);
		$this->display();
	}
	
}
?>